<template>
  <div class="app-container">
    <!-- 查询表单区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="search-form">
      <el-form-item label="支付接口编码" prop="ifCode">
        <el-input
          v-model="queryParams.ifCode"
          placeholder="请输入支付接口编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付接口名称" prop="ifName">
        <el-input
          v-model="queryParams.ifName"
          placeholder="请输入支付接口名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配置页面类型" prop="configPageType">
        <el-select
          v-model="queryParams.configPageType"
          placeholder="请选择配置页面类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in configPageTypeOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <div class="top-operation-buttons">
      <div class="button-group">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
         v-hasPermi="['pay:payIfDefine:add']"
        >新增</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchDelete"
          style="margin-left: 8px"
           v-hasPermi="['pay:payIfDefine:del']"
        >批量删除</el-button>
      </div>
    </div>

    <el-table :data="apiResData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="支付接口编码" align="center" prop="ifCode" />
      <el-table-column label="支付接口名称" align="center" prop="ifName" />
      <el-table-column label="支付参数配置页面类型" align="center" prop="configPageType">
        <template slot-scope="scope">
          {{ scope.row.configPageType === 1 ? '根据接口配置定义描述渲染页面' : '自定义页面' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
             v-hasPermi="['pay:payIfDefine:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
             v-hasPermi="['pay:payIfDefine:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

     <!-- 分页 -->
     <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增页面组件  -->
    <PayIfDefineAddOrEdit ref="payIfDefineAddOrEdit" @refreshList="getList"/>
  </div>
</template>
<script>

import { list, delPayIfDefine, getPayInterface } from '@/api/pay/payconfig/payIfDefine/payIfDefine'
import PayIfDefineAddOrEdit from './AddOrEdit'

export default {
  name: 'IfDefinePage',
  components: {
    PayIfDefineAddOrEdit
  },
  data () {
    return {
      apiResData: [], // 接口返回数据
      iPage: { pageNumber: 1, pageSize: this.pageSize }, // 默认table 分页/排序请求后端格式
      pagination: { total: 0, current: 1, pageSize: this.pageSize, showSizeChanger: true, showTotal: total => `共${total}条` }, // ATable 分页配置项
      showLoading: false,
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      demoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ifCode: undefined,
        ifName: undefined,
        configPageType: undefined,
      },
      // 配置页面类型选项
      configPageTypeOptions: [
        {
          value: 1,
          label: '根据接口配置定义描述渲染页面'
        },
        {
          value: 2,
          label: '自定义页面'
        }
      ],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      /** 查询列表 */
      list(this.addDateRange(this.queryParams, this.dateRange)).then(resData => {
        this.apiResData = resData.rows
        this.total = resData.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.payIfDefineAddOrEdit.reset();
      this.$refs.payIfDefineAddOrEdit.visible = true;
      this.$refs.payIfDefineAddOrEdit.title = "新增支付接口";
      this.$refs.payIfDefineAddOrEdit.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.payIfDefineAddOrEdit.form = row;
      this.$refs.payIfDefineAddOrEdit.visible = true;
      this.$refs.payIfDefineAddOrEdit.title = "修改支付接口";
      this.$refs.payIfDefineAddOrEdit.isAdd = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ifCode = row.ifCode || this.ids;
      this.$modal.confirm('是否确认删除支付接口编码为"' + ifCode + '"的数据项？').then(() => {
        return delPayIfDefine(ifCode);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 批量删除按钮操作
    handleBatchDelete() {
      console.log(this.ids)
      const ifCodes = this.ids
      this.$modal.confirm('是否确认删除选中的支付接口定义？').then(() => {
        this.loading = true
        return delPayIfDefine(ifCodes)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).finally(() => {
        this.loading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ifCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
  }
}
</script>

<style lang="less" scoped>
.top-operation-buttons {
  margin-bottom: 8px;
  display: flex;
  justify-content: flex-end;
  .button-group {
    display: flex;
    gap: 8px;
  }
}
</style>
